'--------------------------------------------------------
' 执行所有测试用例
'--------------------------------------------------------
Private Sub test_StringTokenExtractor_ALL_case()
    ' 基础测试
    test_StringTokenExtractor_exec
    ' 忽略大小写测试
    test_StringTokenExtractor_exec_ignore
    test_StringTokenExtractor_exec2
    ' putBack 数据放回测试
    test_StringTokenExtractor_putBack
    ' 测试最后一位字符能否被分割
    test_StringTokenExtractor_last_char_can_be_cut
    ' addMultiIgnorableDelimiter 测试
    test_StringTokenExtractor_addMultiIgnorableDelimiter
    ' 测试: 分隔符末尾部分相同
    '    Lf
    ' Cr Lf
    test_StringTokenExtractor_vbCrLF_vbLf
End Sub

'======================================
' StringTokenExtractor
'======================================
' 基础测试
Private Sub test_StringTokenExtractor_exec()
    Dim extractor As StringTokenExtractor
    Set extractor = New StringTokenExtractor

    extractor.addIgnorableDelimiter " "
    extractor.addIgnorableDelimiter "aba"
    extractor.addIgnorableDelimiter "abac"
    extractor.addIgnorableDelimiter "abacd"
    extractor.addIgnorableDelimiter "abad"

    extractor.addRetainableDelimiter "1"
    extractor.addRetainableDelimiter "12"
    extractor.addRetainableDelimiter "123"
    extractor.addRetainableDelimiter "124"


    extractor.addSurroundPair "xxx", "yyy"
    extractor.addSurroundPair "xxx", "yyyzzzz"
    extractor.addSurroundPair "xxx", "ttt"

    Dim testStr As String
    testStr = " aba//abac//abacd//abad//1//12//123//124// //xxx1234yyy//xxx1234yyyzzzz//xxx1234ttt//xxx1234xxx"

    Dim result() As String
    Dim resultMax As Integer
    resultMax = 19
    ReDim result(0 To resultMax)
    result(0) = "//"
    result(1) = "//"
    result(2) = "//"
    result(3) = "//"
    result(4) = "1"
    result(5) = "//"
    result(6) = "12"
    result(7) = "//"
    result(8) = "123"
    result(9) = "//"
    result(10) = "124"
    result(11) = "//"
    result(12) = "//"
    result(13) = "xxx1234yyy"
    result(14) = "//"
    result(15) = "xxx1234yyyzzzz"
    result(16) = "//"
    result(17) = "xxx1234ttt"
    result(18) = "//"
    result(19) = "xxx1234xxx"

    extractor.analyze testStr

    Dim index As Integer
    index = 0
    While extractor.hasToken
        Debug.Assert extractor.getNextToken = result(index)
        index = index + 1
    Wend

    Debug.Assert index = (resultMax + 1)
End Sub

' 忽略大小写测试
Private Sub test_StringTokenExtractor_exec_ignore()
    Dim extractor As StringTokenExtractor
    Set extractor = New StringTokenExtractor

    extractor.addIgnorableDelimiter " "
    extractor.addIgnorableDelimiter "aBa", True
    extractor.addIgnorableDelimiter "abac"
    extractor.addIgnorableDelimiter "abacd"
    extractor.addIgnorableDelimiter "abad"

    extractor.addRetainableDelimiter "1"
    extractor.addRetainableDelimiter "12"
    extractor.addRetainableDelimiter "123"
    extractor.addRetainableDelimiter "124"
    extractor.addRetainableDelimiter "12X", True


    extractor.addSurroundPair "xxx", "yyy"
    extractor.addSurroundPair "xxx", "yyyzzzz"
    extractor.addSurroundPair "xxx", "ttt"
    extractor.addSurroundPair "ZxC", "Opx", True, True

    Dim testStr As String
    testStr = " aba//abac//abacd//abad//1//12//123//124//12X// //xxx1234yyy//xxx1234yyyzzzz//xxx1234ttt//zxcdfdfdopxccc"

    Dim result() As String
    Dim resultMax As Integer
    resultMax = 22
    ReDim result(0 To resultMax)
    result(0) = "//"
    result(1) = "//"
    result(2) = "//"
    result(3) = "//"
    result(4) = "1"
    result(5) = "//"
    result(6) = "12"
    result(7) = "//"
    result(8) = "123"
    result(9) = "//"
    result(10) = "124"
    result(11) = "//"
    result(12) = "12X"
    result(13) = "//"
    result(14) = "//"
    result(15) = "xxx1234yyy"
    result(16) = "//"
    result(17) = "xxx1234yyyzzzz"
    result(18) = "//"
    result(19) = "xxx1234ttt"
    result(20) = "//"
    result(21) = "zxcdfdfdopx"
    result(22) = "ccc"

    extractor.analyze testStr

    Dim index As Integer
    index = 0
    While extractor.hasToken
        Debug.Assert extractor.getNextToken = result(index)
        index = index + 1
    Wend

    Debug.Assert index = (resultMax + 1)
End Sub

Private Sub test_StringTokenExtractor_exec2()
    Dim extractor As StringTokenExtractor
    Set extractor = New StringTokenExtractor

    extractor.addIgnorableDelimiter " "
    extractor.addIgnorableDelimiter "aba"
    extractor.addIgnorableDelimiter "abac"
    extractor.addIgnorableDelimiter "abacd"
    extractor.addIgnorableDelimiter "abad"

    extractor.addRetainableDelimiter "1"
    extractor.addRetainableDelimiter "12"
    extractor.addRetainableDelimiter "123"
    extractor.addRetainableDelimiter "124"


    extractor.addSurroundPair "xxx", "yyy"
    extractor.addSurroundPair "xxx", "yyyzzzz"
    extractor.addSurroundPair "xxx", "ttt"

    Dim testStr As String
    testStr = " //yyyyyyyyyyyy"

    Dim result() As String
    Dim resultMax As Integer
    resultMax = 0
    ReDim result(0 To resultMax)
    result(0) = "//yyyyyyyyyyyy"

    extractor.analyze testStr

    Dim index As Integer
    index = 0
    While extractor.hasToken
        Debug.Assert extractor.getNextToken = result(index)
        index = index + 1
    Wend

    Debug.Assert index = (resultMax + 1)
End Sub

' putBack 数据放回测试
Private Sub test_StringTokenExtractor_putBack()
    Dim extractor As StringTokenExtractor
    Set extractor = New StringTokenExtractor

    extractor.addIgnorableDelimiter " "
    extractor.addIgnorableDelimiter "aba"
    extractor.addIgnorableDelimiter "abac"
    extractor.addIgnorableDelimiter "abacd"
    extractor.addIgnorableDelimiter "abad"

    extractor.addRetainableDelimiter "1"
    extractor.addRetainableDelimiter "12"
    extractor.addRetainableDelimiter "123"
    extractor.addRetainableDelimiter "124"


    extractor.addSurroundPair "xxx", "yyy"
    extractor.addSurroundPair "xxx", "yyyzzzz"
    extractor.addSurroundPair "xxx", "ttt"

    Dim testStr As String
    testStr = " aba//abac//abacd//abad//1//12//123//124// //xxx1234yyy//xxx1234yyyzzzz//xxx1234ttt//xxx1234xxx"

    Dim result() As String
    Dim resultMax As Integer
    resultMax = 19
    ReDim result(0 To resultMax)
    result(0) = "//"
    result(1) = "//"
    result(2) = "//"
    result(3) = "//"
    result(4) = "1"
    result(5) = "//"
    result(6) = "12"
    result(7) = "//"
    result(8) = "123"
    result(9) = "//"
    result(10) = "124"
    result(11) = "//"
    result(12) = "//"
    result(13) = "xxx1234yyy"
    result(14) = "//"
    result(15) = "xxx1234yyyzzzz"
    result(16) = "//"
    result(17) = "xxx1234ttt"
    result(18) = "//"
    result(19) = "xxx1234xxx"

    extractor.analyze testStr

    Dim index As Integer
    index = 0
    While extractor.hasToken
        Debug.Assert extractor.getNextToken = result(index)
        index = index + 1

        extractor.putBack "xxxx"
        extractor.putBack "yyyy"

        Debug.Assert extractor.getNextToken = "yyyy"
        Debug.Assert extractor.getNextToken = "xxxx"
    Wend

    Debug.Assert index = (resultMax + 1)
End Sub

' 测试最后一位字符能否被分割
Private Sub test_StringTokenExtractor_last_char_can_be_cut()
    Dim extractor As StringTokenExtractor
    Set extractor = New StringTokenExtractor

    extractor.addIgnorableDelimiter " "

    Dim testStr As String
    testStr = "limit 2"

    Dim result() As String
    Dim resultMax As Integer
    resultMax = 1
    ReDim result(0 To resultMax)
    result(0) = "limit"
    result(1) = "2"

    extractor.analyze testStr

    Dim index As Integer
    index = 0
    While extractor.hasToken
        Debug.Assert extractor.getNextToken = result(index)
        index = index + 1
    Wend

    Debug.Assert index = (resultMax + 1)
End Sub

' addMultiIgnorableDelimiter 测试
Private Sub test_StringTokenExtractor_addMultiIgnorableDelimiter()
    Dim extractor As StringTokenExtractor
    Set extractor = New StringTokenExtractor

    'extractor.addIgnorableDelimiter vbTab
    'extractor.addIgnorableDelimiter vbCrLf
    'extractor.addIgnorableDelimiter vbLf

    extractor.addMultiIgnorableDelimiter Array(vbTab, vbCrLf)

    Dim testStr As String
    testStr = "abc" & vbCrLf & " xx xyz"

    Dim result() As String
    Dim resultMax As Integer
    resultMax = 1
    ReDim result(0 To resultMax)
    result(0) = "abc"
    result(1) = " xx xyz"

    extractor.analyze testStr

    Dim index As Integer
    index = 0
    While extractor.hasToken
        Debug.Assert extractor.getNextToken = result(index)
        index = index + 1
    Wend

    Debug.Assert index = (resultMax + 1)
End Sub

' 测试: 分隔符末尾部分相同
'    Lf
' Cr Lf
Private Sub test_StringTokenExtractor_vbCrLF_vbLf()
    Dim extractor As StringTokenExtractor
    Set extractor = New StringTokenExtractor

    extractor.addMultiIgnorableDelimiter Array(vbCrLf, vbLf)

    Dim testStr As String
    testStr = "abc" & vbCrLf & " xx" & vbLf & "xyz"

    Dim result() As String
    Dim resultMax As Integer
    resultMax = 2
    ReDim result(0 To resultMax)
    result(0) = "abc"
    result(1) = " xx"
    result(2) = "xyz"

    extractor.analyze testStr

    Dim index As Integer
    index = 0
    While extractor.hasToken
        Debug.Assert extractor.getNextToken = result(index)
        index = index + 1
    Wend

    Debug.Assert index = (resultMax + 1)
End Sub
